package com.alks.function.controller.finance.ryfinance;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.YfApAmt;
import com.alks.entity.data.entity.YfApAmtFk;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.finance.ryfinance.ApAmtHeadDto;
import com.alks.function.data.request.finance.ryfinance.ApAmtHeadRequest;
import com.alks.function.data.vo.finance.mtlcosts.YfApExportVo;
import com.alks.function.service.finance.ryfinance.IApAmtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author MS
 * @since 2024/5/27下午5:09
 */
@RestController
@ApiRestController("/ryFinance")
@Api(value = "财务部", tags = "财务部-荣阳财务-结算应付作业")
@Slf4j
public class ApAmtController {
    @Autowired
    private IApAmtService service;

    @WebLogger
    @PostMapping("/apAmtHeadQuery")
    @ApiOperation(value = "结算资料查询-分页可条件查询", tags = "财务部-荣阳财务-结算应付作业")
    public ResponseInfo apAmtHeadQuery(@RequestBody ApAmtHeadRequest request) {
        Map<String, Object> map = service.apAmtHeadQuery(request);
        return ResponseInfo.ok(map);
    }

    @WebLogger
    @PutMapping("/apAmtHeadAdd")
    @ApiOperation(value = "结算-新增，修改", tags = "财务部-荣阳财务-结算应付作业")
    public ResponseInfo apAmtHeadAdd(@RequestParam@ApiParam(required = true,value = "结算月份") String financeYymm,
                                     @RequestParam@ApiParam(required = true,value = "厂商")  String vendorName) {
        return service.apAmtHeadAdd(financeYymm,vendorName);
    }

    @WebLogger
    @PutMapping("/apAmtHeadDel")
    @ApiOperation(value = "取消结算-删除，修改", tags = "财务部-荣阳财务-结算应付作业")
    public ResponseInfo apAmtHeadDel(@RequestParam@ApiParam(required = true,value = "单据号码")  String slipNo) {
        return service.apAmtHeadDel(slipNo);
    }

    @WebLogger
    @PostMapping("/apAmtDetailQuery")
    @ApiOperation(value = "结算明细查询-分页可条件查询", tags = "财务部-荣阳财务-结算应付作业")
    public ResponseInfo apAmtDetailQuery(@RequestBody PageParam request,
                                         @RequestParam@ApiParam(required = true,value = "单据号码")  String slipNo) {
        Map<String, Object> map = service.apAmtDetailQuery(request, slipNo,false);
        return ResponseInfo.ok(map);
    }

    @WebLogger
    @PutMapping("/apAmtCfm")
    @ApiOperation(value = "结算明细检核-修改", tags = "财务部-荣阳财务-结算应付作业")
    public ResponseInfo apAmtCfm(@RequestBody@ApiParam(required = true,value = "单据号码") List<String> slipNos,
                                 @RequestParam(required = false)@ApiParam(value = "Y.检核 null.取消检核")  String cfmFlag) {
        return service.apAmtCfm(slipNos,cfmFlag);
    }

    @WebLogger
    @PostMapping("/apAmtFkQuery")
    @ApiOperation(value = "付款记录查询-条件查询", tags = "财务部-荣阳财务-结算应付作业")
    public ResponseInfo apAmtFkQuery(@RequestParam@ApiParam(required = true,value = "单据号码")  String slipNo) {
        return service.apAmtFkQuery(slipNo);
    }

    @WebLogger
    @PutMapping("/apAmtFkUp")
    @ApiOperation(value = "付款记录修改-修改、新增", tags = "财务部-荣阳财务-结算应付作业")
    public ResponseInfo apAmtFkUp(@RequestBody List<YfApAmtFk> request) {
        return service.apAmtFkUp(request);
    }


    @WebLogger
    @PostMapping("/apAmtFkDel")
    @ApiOperation(value = "付款记录删除-删除", tags = "财务部-荣阳财务-结算应付作业")
    public ResponseInfo apAmtFkDel(@RequestParam@ApiParam(required = true,value = "单据号码")  String slipNo,
                                   @RequestParam@ApiParam(required = true,value = "序号") BigDecimal item) {
        return service.apAmtFkDel(slipNo,item);
    }

    @WebLogger
    @PutMapping("/aftPayProcess")
    @ApiOperation(value = "尾款处理-新增", tags = "财务部-荣阳财务-结算应付作业")
    public ResponseInfo aftPayProcess(@RequestBody@ApiParam("订单号") List<String> slipNos) {
        return service.aftPayProcess(slipNos);
    }

    @WebLogger
    @PostMapping("/apAmtHead/export")
    @ApiOperation(value = "明细导出-导出EXCEL", tags = "财务部-荣阳财务-结算应付作业")
    public void export(@RequestBody ApAmtHeadRequest request, HttpServletResponse response) {
        /*取消分页*/
        request.setPageNum(-1);
        request.setPageSize(-1);
        /*获取数据*/
        List<ApAmtHeadDto> dtos = (List<ApAmtHeadDto>) service.apAmtHeadQuery(request).get("dtos");
        if (dtos.isEmpty()){
            throw new ServiceErrorException("未能查到要导出的数据");
        }
        /*处理合计*/
        BigDecimal amtSum = dtos.stream().map(ApAmtHeadDto::getAmt).reduce(BigDecimal.ZERO, BigDecimal::add);
        ApAmtHeadDto total = new ApAmtHeadDto();
        total.setAmt(amtSum);
        total.setFinanceYymm("合计");
        dtos.add(total);
        /*导出*/
        String templetName = "荣阳结算应付模版.xlsx";
        String excelName = "荣阳结算应付"+ DateUtils.dateTimeNow();
        try {
            ExcelUtils.creatByTemplate(response,templetName,excelName, dtos);
        } catch (IOException e) {
            throw new ServiceErrorException("导出失败");
        }
    }

    @WebLogger
    @PostMapping("/apAmtDetail/smr/export")
    @ApiOperation(value = "汇总导出-导出EXCEL", tags = "财务部-荣阳财务-结算应付作业")
    public void detailsmrExport(@RequestBody PageParam request,
                                @RequestParam String slipNo,
                                HttpServletResponse response) {
        /*取消分页*/
        request.setPageNum(-1);
        request.setPageSize(-1);
        /*获取数据*/
        Map<String, Object> map = service.apAmtDetailQuery(request, slipNo, true);
        List<YfApAmt> dtos = (List<YfApAmt>) map.get("dtos");
        if (dtos.isEmpty()){
            throw new ServiceErrorException("未能查到要导出的数据");
        }
        /*处理合计*/
        YfApAmt total = new YfApAmt();
        total.setAmt((BigDecimal)map.get("amtSum"));
        total.setQty((BigDecimal)map.get("qtySum"));
        total.setMtlUnit("合计");
        dtos.add(total);
        /*导出*/
        String templetName = "荣阳结算汇总模版.xls";
        String excelName = "荣阳结算汇总"+ DateUtils.dateTimeNow()+".xls";
        try {
            ExcelUtils.autoWidth().creatByTemplate(response,templetName,excelName, dtos);
        } catch (IOException e) {
            throw new ServiceErrorException("导出失败");
        }
    }

    @WebLogger
    @PostMapping("/apAmtDetail/export")
    @ApiOperation(value = "明细导出-导出EXCEL", tags = "财务部-荣阳财务-结算应付作业")
    public void detailExport(@RequestBody PageParam request,
                                @RequestParam String slipNo,
                                HttpServletResponse response) {
        /*取消分页*/
        request.setPageNum(-1);
        request.setPageSize(-1);
        /*获取数据*/
        List<YfApAmt> dtos = (List<YfApAmt>) service.apAmtDetailQuery(request,slipNo,false).get("dtos");
        if (dtos.isEmpty()){
            throw new ServiceErrorException("未能查到要导出的数据");
        }
        List<YfApExportVo> voList = service.detExpAdtCustom(dtos);
        /*处理合计*/
        BigDecimal amtSum = voList.stream()
                .map(YfApAmt::getAmt)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal qtySum = voList.stream()
                .map(YfApAmt::getQty)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        YfApExportVo total = new YfApExportVo();
        total.setAmt(amtSum);
        total.setQty(qtySum);
        total.setMtlUnit("合计");
        voList.add(total);
        /*导出*/
        String templetName = "荣阳结算明细模版.xls";
        String excelName = "荣阳结算明细"+ DateUtils.dateTimeNow()+".xls";
        try {
            ExcelUtils.autoWidth().creatByTemplate(response,templetName,excelName, voList);
        } catch (IOException e) {
            throw new ServiceErrorException("导出失败");
        }
    }
}
